//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package edu.scau.mis.web.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.scau.mis.cloth.entity.GsClothes;
import edu.scau.mis.cloth.service.GsAttrsService;
import edu.scau.mis.cloth.service.GsDiscountService;
import edu.scau.mis.cloth.service.GsOrderItemService;
import edu.scau.mis.cloth.service.GsOrderService;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping({"gsOrderItem"})
public class GsOrderItemController {
    @Autowired
    private GsOrderItemService gsOrderItemService;
    @Autowired
    private GsOrderService gsOrderService;
    @Autowired
    private GsDiscountService gsDiscountService;
    @Autowired
    private GsAttrsService gsAttrsService;

    public GsOrderItemController() {
    }

    @GetMapping({"{id}"})
    public List<Map<String, Object>> queryOrderById(@PathVariable("id") String id) {
        return this.gsOrderItemService.queryByOrderId(id);
    }

    @GetMapping({"addItem/{orderId}/{clId}/{clSize}"})
    public String insertItem(@PathVariable("orderId") String orderId, @PathVariable("clId") Integer clId, @PathVariable("clSize") String clSize) {
        int a = this.gsAttrsService.getInventory(clId, clSize);
        if (a > 0) {
            this.gsOrderItemService.insert(orderId, clId, clSize);
            this.gsAttrsService.updateInventory(clId, clSize, a - 1);
            this.gsDiscountService.updateDiscount(orderId, clId);
            this.gsOrderService.update(orderId);
            return "success";
        } else {
            return "error！库存不足";
        }
    }

    @GetMapping("/countByCategory")
    public List<Map<String,Object>> countByCategory(){
        return gsOrderItemService.countByCategory();
    }

    @GetMapping("/countByName")
    public PageInfo countByName(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Map<String,Object>> list = gsOrderItemService.countByName();
        PageInfo<List> pageInfo = new PageInfo(list);
        return pageInfo;
    }
    @GetMapping("/echName")
    public List<Map<String,Object>> echName(){
        return gsOrderItemService.echName();
    }

    @GetMapping("/echCategory")
    public List<Map<String,Object>> echCategory(){
        return gsOrderItemService.echCategory();
    }

}
